package com.atguigu.shiro.Web;

import com.atguigu.shiro.entity.User;
import com.atguigu.shiro.response.WebResponse;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

@RequestMapping("/mongodb")
@RestController
public class MongodbController {
    //直接调用MongoTemplate实现增加改查
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 新增用户
     */
    @ApiOperation(value = "新增用户",notes = "新增100个用户")
    @PostMapping("/add")
    public WebResponse add() throws ParseException {
        List<User> userList = new ArrayList<>();
        User user = null;
        for (int i = 1; i <=100 ; i++) {
            user = new User();
            user.setName("账户");
            user.setSex(i%2==0?1:0);
            user.setBrithday(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            user.setAddress("江苏省 苏州市");
            userList.add(user);
            mongoTemplate.insert(user);
        }
        return WebResponse.resSuccess("新增成功",userList.size());
    }

    /**
     * 删除语句
     */
    @ApiOperation(value = "删除语句",notes = "根据_id来删除")
    @ApiImplicitParam(name = "id",value = "id值",required = true,dataType = "String")
    @DeleteMapping("/delete")
    public WebResponse delete(String id){
        //查询
        Query query = new Query();
        //条件
        Criteria criteria = new Criteria();
        criteria.and("_id").is(id);
        query.addCriteria(criteria);

        DeleteResult deleteResult = mongoTemplate.remove(query,User.class);
        return WebResponse.resSuccess("删除成功",deleteResult.getDeletedCount());
    }

    /**
     * 修改用户信息
     */
    @ApiOperation(value = "修改用户信息",notes = "根据_id和name来修改name")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id值",required = true,dataType = "String"),
            @ApiImplicitParam(name = "name",value = "name",required = true,dataType = "String"),
    })
    @PutMapping("/update")
    public WebResponse update(String id,String name){
        //查询
        Query query = new Query();
        //条件
        Criteria criteria = new Criteria();
        criteria.and("_id").is(id);
        query.addCriteria(criteria);

        Update update = new Update();
        update.set("name",name);

        UpdateResult updateResult = mongoTemplate.updateMulti(query, update, User.class);
        return WebResponse.resSuccess("修改成功",updateResult.getModifiedCount());
    }

    /**
     * 查询列表
     */
    @ApiOperation(value = "查询列表",notes = "根据条件来查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "名字",required = false,dataType = "String"),
            @ApiImplicitParam(name = "sex",value = "性别0:男1:女",required = false,dataType = "int"),
            @ApiImplicitParam(name = "brithday",value = "生日",required = false,dataType = "String")
    })
    @GetMapping("/list")
    public WebResponse list(String name,Integer sex,String brithday){
        //查询
        Query query = new Query();
        //条件
        Criteria criteria = new Criteria();

        //姓名 模糊查询
        Pattern pattern = null;
        if(!StringUtils.isEmpty(name)){
            //用正则匹配进行模糊查询
            pattern = Pattern.compile("^.*" + name + ".*$", Pattern.CASE_INSENSITIVE);
            criteria.and("name").regex(pattern);
        }
        if(null!=sex){
            criteria.and("sex").is(sex);
        }
        if(!StringUtils.isEmpty(brithday)){
            criteria.and("brithday").is(brithday);
        }
        query.addCriteria(criteria);

        //统计查询总条数
        long count = mongoTemplate.count(query, User.class);
        System.out.println("统计查询总条数"+count);

        return WebResponse.resSuccess("查询成功",mongoTemplate.find(query, User.class));
    }
}
